package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.SalesInvoiceMapper;
import com.ruoyi.crm.domain.SalesInvoice;
import com.ruoyi.crm.service.ISalesInvoiceService;

/**
 * 销售发票管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-01
 */
@Service
public class SalesInvoiceServiceImpl implements ISalesInvoiceService
{
    @Autowired
    private SalesInvoiceMapper salesInvoiceMapper;

    /**
     * 查询销售发票管理
     *
     * @param id 销售发票管理主键
     * @return 销售发票管理
     */
    @Override
    public SalesInvoice selectSalesInvoiceById(String id)
    {
        return salesInvoiceMapper.selectSalesInvoiceById(id);
    }

    /**
     * 查询销售发票管理列表
     *
     * @param salesInvoice 销售发票管理
     * @return 销售发票管理
     */
    @Override
    public List<SalesInvoice> selectSalesInvoiceList(SalesInvoice salesInvoice)
    {
        return salesInvoiceMapper.selectSalesInvoiceList(salesInvoice);
    }

    /**
     * 新增销售发票管理
     *
     * @param salesInvoice 销售发票管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSalesInvoice(SalesInvoice salesInvoice)
    {
        int rows = salesInvoiceMapper.insertSalesInvoice(salesInvoice);
        insertCustomerProduct(salesInvoice);
        return rows;
    }

    /**
     * 修改销售发票管理
     *
     * @param salesInvoice 销售发票管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSalesInvoice(SalesInvoice salesInvoice)
    {
        salesInvoice.setUpdateTime(DateUtils.getNowDate());
        salesInvoiceMapper.deleteCustomerProductByCustomerName(salesInvoice.getId());
        insertCustomerProduct(salesInvoice);
        return salesInvoiceMapper.updateSalesInvoice(salesInvoice);
    }

    /**
     * 批量删除销售发票管理
     *
     * @param ids 需要删除的销售发票管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalesInvoiceByIds(String[] ids)
    {
        return salesInvoiceMapper.deleteSalesInvoiceByIds(ids);
    }

    /**
     * 删除销售发票管理信息
     *
     * @param id 销售发票管理主键
     * @return 结果
     */
    @Override
    public int deleteSalesInvoiceById(String id)
    {
        salesInvoiceMapper.deleteCustomerProductByCustomerName(id);
        return salesInvoiceMapper.deleteSalesInvoiceById(id);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param salesInvoice 销售发票管理对象
     */
    public void insertCustomerProduct(SalesInvoice salesInvoice)
    {
        List<CustomerProduct> customerProductList = salesInvoice.getCustomerProductList();
        String id = salesInvoice.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                salesInvoiceMapper.batchCustomerProduct(list);
            }
        }
    }
}
